package algorithm.util;

import algorithm.util.BigInteger;


/**
 * Fraction
 * @author wong.tong@gmail.com
 *
 */
public class Fraction {
	
	private BigInteger numerator;
	private BigInteger denominator;
	
	public Fraction(String frac) {
		
		if (frac == null || frac.trim().length() == 0) {
			this.numerator = new BigInteger("0");
			this.denominator = new BigInteger("1");
		} else {
			int idx = frac.indexOf("/");
			if (idx > -1) {
				String[] ff = frac.split("/");
				this.numerator = new BigInteger(ff[0]);
				this.denominator = new BigInteger(ff[1]);
			} else {
				this.numerator = new BigInteger(frac);
				this.denominator = new BigInteger("1");
			}
		}
	}
	
	public Fraction(BigInteger n, BigInteger d) {
		
		this.numerator = n;
		this.denominator = d;
	}
	
	public Fraction add(Fraction f) {
		BigInteger n = this.numerator.mul(f.denominator).add(this.denominator.mul(f.numerator));
		BigInteger d = this.denominator.mul(f.denominator); 
		return new Fraction(n, d);
	}
	
	@Override
	public String toString() {
		return this.numerator + "/" + this.denominator;
	}

	public BigInteger getDenominator() {
		return denominator;
	}

	public void setDenominator(BigInteger denominator) {
		this.denominator = denominator;
	}

	public BigInteger getNumerator() {
		return numerator;
	}

	public void setNumerator(BigInteger numerator) {
		this.numerator = numerator;
	}
}